//
//  XZBaseNetwork.m
//  XZNetworking
//
//  Created by Tony on 2016/11/30.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "XZBaseNetwork.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
static XZBaseNetwork *baseNetworkObjc;
@implementation XZBaseNetwork

+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        baseNetworkObjc = [[XZBaseNetwork alloc] init];
    });
    return baseNetworkObjc;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        baseNetworkObjc = [super allocWithZone:zone];
    });
    return baseNetworkObjc;
}

- (id)copyWithZone:(NSZone *)zone
{
    return baseNetworkObjc;
}
- (id)mutableCopyWithZone:(nullable NSZone *)zone
{
    return baseNetworkObjc;
}

- (void)startWithGetURL:(NSString *)url params:(NSDictionary *)params success:(SuccessBlock)successBase failure:(FailureBlock)failureBase
{
    [SVProgressHUD showWithStatus:@"Loading"];
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer  = [AFHTTPResponseSerializer serializer];
   
   NSURLSessionDataTask *task = [sessionManager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功");
       [SVProgressHUD dismiss];
       id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
       
        successBase(result);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        failureBase(error);
        NSLog(@"失败");
    }];
    // 启动任务
    [task resume];
}
- (void)startWithPostURL:(NSString *)url params:(NSDictionary *)params success:(SuccessBlock)successBase failure:(FailureBlock)failureBase
{
    [SVProgressHUD showWithStatus:@"Loading"];
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    sessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    NSURLSessionDataTask *task = [sessionManager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        NSLog(@"%@",result);
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    [task resume];
}

@end
